package com.store.pet.petstore.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.store.pet.petstore.R;
import com.store.pet.petstore.activity.PetDetailActivity;
import com.store.pet.petstore.activity.PetListActivity;
import com.store.pet.petstore.activity.SearchActivity;
import com.store.pet.petstore.adapter.CommodityAdapter;
import com.store.pet.petstore.adapter.InStockAdapter;
import com.store.pet.petstore.bean.CommodityBean;
import com.store.pet.petstore.bean.PetListBean;
import com.store.pet.petstore.util.Constant;
import com.store.pet.petstore.util.GsonUtils;
import com.store.pet.petstore.util.OKHttpUtils;
import com.store.pet.petstore.util.SpaceItemDecoration;
import com.store.pet.petstore.util.mLocalGlideImageLoader;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.store.pet.petstore.activity.PetListActivity.BUY_SUCCESS;

/**
 * 首页Fragment
 */
public class HomeFragment extends BaseFragment {

    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.dog)
    TextView dog;
    @BindView(R.id.cat)
    TextView cat;
    @BindView(R.id.pet)
    TextView pet;
    @BindView(R.id.recycler_inStock)
    RecyclerView recyclerInStock;

    private List<Integer> list = new ArrayList<>();  //轮播图数据集合

    private List<PetListBean.ContentBean.PetlistBean> petlistBeans = new ArrayList<>();  //在售宠物集合

    private InStockAdapter adapter;

    @Override
    protected int getLayoutID() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        //设置轮播图数据
        list.add(R.drawable.banner1);
        list.add(R.drawable.banner2);
        list.add(R.drawable.banner3);
        list.add(R.drawable.banner4);
        //设置banner样式
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        banner.setImageLoader(new mLocalGlideImageLoader());
        //设置图片集合
        banner.setImages(list);
        //设置ban效果ner动画
        banner.setBannerAnimation(Transformer.Default);
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(3000);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
        banner.start();

        /**
         * 每行2个数据的列表
         */
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        recyclerInStock.setLayoutManager(layoutManager);
        recyclerInStock.addItemDecoration(new SpaceItemDecoration(30));
        adapter = new InStockAdapter(R.layout.item_instock_rv,petlistBeans);
        recyclerInStock.setAdapter(adapter);
        adapter.setOnItemChildClickListener((adapter, view, position) -> {
            switch (view.getId()){
                case R.id.instock_item:
                    Intent intent = new Intent(getActivity(),PetDetailActivity.class);
                    intent.putExtra("pet",petlistBeans.get(position));
                    startActivityForResult(intent,BUY_SUCCESS);
                    break;
            }
        });
        loadData();
    }

    public void loadData() {
        petlistBeans.clear();
        OKHttpUtils.requestData(Constant.INSTOCK, new HashMap<>(), new OKHttpUtils.ServerCallBack() {
            @Override
            public void onSuccess(String data) {
                PetListBean bean = GsonUtils.parsePetListBean(data);
                if(bean.getContent().getPetlist().size() != 0){
                    petlistBeans.addAll(bean.getContent().getPetlist());
                }
                new Thread(() -> getActivity().runOnUiThread(() -> adapter.notifyDataSetChanged())).start();  //在主线程刷新界面UI
            }

            @Override
            public void onFailed(String message) {

            }
        });
    }

    @OnClick({R.id.dog, R.id.cat, R.id.pet, R.id.search})
    public void onViewClicked(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.dog:
                intent.setClass(getActivity(), PetListActivity.class);
                intent.putExtra("type", 1);
                startActivity(intent);
                break;
            case R.id.cat:
                intent.setClass(getActivity(), PetListActivity.class);
                intent.putExtra("type", 2);
                startActivity(intent);
                break;
            case R.id.pet:
                intent.setClass(getActivity(), PetListActivity.class);
                intent.putExtra("type", 3);
                startActivity(intent);
                break;
            case R.id.search:
                //搜索
                intent.setClass(getActivity(),SearchActivity.class);
                startActivity(intent);
                break;
        }

    }
}
